import 'package:archive/archive_io.dart';
import 'package:archive/archive.dart';
import 'package:flutter/cupertino.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:permission_handler/permission_handler.dart';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:heyangkuaidai/FrameWorkModule/hotfix/FileManager.dart';
import 'package:heyangkuaidai/BussinessModule/BussinessToast/PublicAssignedToOrganToast.dart';
import 'package:heyangkuaidai/FrameWorkModule/global_store/GlobalConstant.dart';

import 'package:heyangkuaidai/DemonStrateModule/FaceRecPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/FirstPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanApplyPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanBindCardPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanCertificationPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanContractSignPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanEnterModalPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanFirstPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanLimitCheckFinshPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanLimitFindPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanProtocalPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSignFinshPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSubscribePage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSubscribeResultPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/HoldLoanPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/SelectRepayPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanDetailPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanRepayPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanRepayConfirmPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanRepayConfirmVSecretPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/RepaySuccessPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanApplySuccessPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/SubscribeResultPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSendConfirmPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/SubscribeCancelPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/PreLoanSendPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSendConfirmVSceretPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSendApplyPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoanSelfHelpSendPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/LoginPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/HomePage.dart';
import 'package:heyangkuaidai/DemonStrateModule/AccountFindPage.dart';
import 'package:heyangkuaidai/DemonStrateModule/AccountFindDetailPage.dart';
// void main() => runApp(flutterTheme());
void main() => runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
       home: LoginPage(),
      routes: <String, WidgetBuilder> {
        // 这里可以定义静态路由，不能传递参数
        "/LoginPage":(context) =>LoginPage(),
        "/AccountFindPage":(context)=>AccountFindPage(),
        "/AccountFindDetailPage":(context)=>AccountFindDetailPage(),
        "/HomePage":(context) =>HomePage(),
        "/FirstPage": (context) => FirstPage(),
        "/LoanApplySuccessPage":(context)=>LoanApplySuccessPage(),
        "/HoldLoanPage":(context)=>HoldLoanPage(),
        "/LoanSendApplyPage":(context)=>LoanSendApplyPage(),
        "/LoanSelfHelpSendPage":(context)=>LoanSelfHelpSendPage(),
        "/LoanSendConfirmVSecretPage":(context)=>LoanSendConfirmVSecretPage(),
        "/LoanSendConfirmPage":(context)=>LoanSendConfirmPage(),
        "/SubscribeCancelPage":(context)=>SubscribeCancelPage(),
        "/LoanDetailPage":(context)=>LoanDetailPage(),
        "/PreLoanSendPage":(context)=>PreLoanSendPage(),
        "/LoanRepayConfirmPage":(context)=>LoanRepayConfirmPage(),
        '/LoanRepayConfirmVSecretPage':(context)=>LoanRepayConfirmVSecretPage(),
        '/RepaySuccessPage':(context)=>RepaySuccessPage(),
        "/LoanRepayPage":(context)=>LoanRepayPage(),
        "/SelectRepayPage":(context)=>SelectRepayPage(),
        "/LoanBindCardPage": (context) => LoanBindCardPage(),
        "/FaceRecPage": (context) => FaceRecPage(),
        "/LoanApplyPage": (context) => LoanApplyPage(),
        "/LoanCertificationPage": (context) => LoanCertificationPage(),
        "/LoanEnterModalPage": (context) => LoanEnterModalPage(),
        "/LoanFirstPage": (context) => LoanFirstPage(),
        "/LoanLimitCheckFinshPage": (context) => LoanLimitCheckFinshPage(),
        "/LoanLimitFindPage": (context) => LoanLimitFindPage(),
        "/LoanProtocalPage": (context) => LoanProtocalPage(),
        "/LoanSignFinshPage": (context) => LoanSignFinshPage(),
        "/LoanSubscribePage": (context) => LoanSubscribePage(),
        "/LoanContractSignPage": (context) => LoanContractSignPage(),
        "/LoanSubscribeResultPage": (context) => LoanSubscribeResultPage(),
      },
    )
);


class WebViewExample extends StatefulWidget {
  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  final Completer<WebViewController> _controller =
  Completer<WebViewController>();

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }
  FileManager manager =FileManager();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter WebView example'),
      ),
      body:FutureBuilder<String>(
          future:manager.unZipRemoveFile(),
          builder: (context, snapshot) {
            print('snapshot+$snapshot');
            if (snapshot.hasData) {
              return WebView(
                initialUrl:'file://'+snapshot.data+ '/out/dist/index.html',
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (WebViewController webViewController) {
                  _controller.complete(webViewController);
                  // webViewController.loadUrl('assets/index.html');
                  print(snapshot.data+'/out/dist/index.html');
                  // webViewController.loadUrl('file://'+snapshot.data+ '/out/test/index.html');
                },
                onProgress: (int progress) {
                  print("WebView is loading (progress : $progress%)");
                },
                javascriptChannels: <JavascriptChannel>{
                  _toasterJavascriptChannel(context),
                },
                navigationDelegate: (NavigationRequest request) {
                  if (request.url.startsWith('https://www.youtube.com/')) {
                    print('blocking navigation to $request}');
                    return NavigationDecision.prevent;
                  }
                  print('allowing navigation to $request');
                  return NavigationDecision.navigate;
                },
                onPageStarted: (String url) {
                  print('Page started loading: $url');
                },
                onPageFinished: (String url) {
                  print('Page finished loading: $url');
                },
                gestureNavigationEnabled: true,
              );
            }else if (snapshot.hasError) {
              return Scaffold(
                body: Center(
                  child: Text("${snapshot.error}"),
                ),
              );
            }
            return Scaffold(
              body: Center(child: CircularProgressIndicator()),
            );
          }
      ),
      floatingActionButton: favoriteButton(),
    );
  }

  JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
    return JavascriptChannel(
        name: 'Toaster',
        onMessageReceived: (JavascriptMessage message) {
          // ignore: deprecated_member_use
          Scaffold.of(context).showSnackBar(
            SnackBar(content: Text(message.message)),
          );
        });
  }

  Widget favoriteButton() {
    return FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context,
            AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
              onPressed: () async {
                // final String url = (await controller.data.currentUrl());
                // // ignore: deprecated_member_use
                // Scaffold.of(context).showSnackBar(
                //     SnackBar(content: Text('Favorited $url')),
                // );
                showDialog(
                    context: context,
                    builder: (context){
                      return PublicAssignedToOrganToast(
                        onCloseEvent: (){
                          Navigator.pop(context);
                        },
                        title: '转换至机构',

                      );
                    }
                );
              },
              child: const Icon(Icons.favorite),
            );
          }
          return Container();
        });
  }
}




